<template>
  <div class="search-history">
    <!-- 搜索历史标题项 -->
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="$$emit('clear-search-histroies')">全部删除</span
        >&nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon name="delete" v-else @click="isDeleteShow = true" />
    </van-cell>
    <!-- 搜索历史记录项 -->
    <van-cell
      :title="item"
      v-for="(item, index) in searchHistories"
      :key="index"
      @click="onSearchItemClick(item, index)"
    >
      <van-icon name="close" v-if="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {

  },
  // props 数据可以修改 但绝不能赋值 不能修改父组件中的数据 只能通过调用自定义属性让父组件修改
  props: {
    searchHistories: {
      type: Array,
      require: true
    }
  },
  data () {
    return {
      isDeleteShow: false // 控制删除图标显示隐藏
    }
  },
  created () {

  },
  methods: {
    // 删除操作
    onSearchItemClick (item, index) {
      // 判断是否是删除状态 是就删除
      if (this.isDeleteShow) {
        this.searchHistories.splice(index, 1)
      } else {
        // 不是删除状态 就点击进入搜索
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style>
</style>
